﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using CommandBinding;
namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBudget.xaml
    /// </summary>
    public partial class VMTabBudget : UserControl
    {
        public VMTabBudget()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(VMTabBudget_Loaded);
        
            SetupBindingCommand();
        }

        void VMTabBudget_Loaded(object sender, RoutedEventArgs e)
        {
            Step_Budget Budget = this.DataContext as Step_Budget;
            if (Budget != null)
            {
                Budget.RefreshParent();
                if (Budget != null)
                {
                    Step_Budgets = new VMTabBudget_Data(Budget);
                    TreeView_root.ItemsSource = Step_Budgets.Step_Budgets;
                }
                else
                {
                    this.DataContext = null;
                    TreeView_root.ItemsSource = null;
                }
                VMTextBox_Number_Cost.LostFocus -= new RoutedEventHandler(VMTextBox_Number_Cost_LostFocus);
                VMTextBox_Number_Cost.LostFocus += new RoutedEventHandler(VMTextBox_Number_Cost_LostFocus);
                VMTabBudgetChart_Item_DrawChart();
            }
        }

        void VMTextBox_Number_Cost_LostFocus(object sender, RoutedEventArgs e)
        {
            VMTabBudgetChart_Item_DrawChart(); ;
        }
        private void SetupBindingCommand()
        {
            this.AssignCondition(Button_AddNew, (s, e) =>
            {
                Step_Budget selected = TreeView_root.SelectedItem as Step_Budget;
                Step_Budgets.Step_Budget_Selected = selected;
                Step_Budget itemNew = new Step_Budget() { Name = "New Node", Cost = 0 };
                Step_Budgets.Add_Step_Budget(itemNew);
                VMTabBudgetChart_Item_DrawChart();
            }, (s, e) =>
            {
                e.OnCanExecute_SyncButtonVisibility(true, TreeView_root.SelectedItem != null);
            });
            this.AssignCondition(Button_Remove, (s, e) =>
            {
                if (MessageBox.Show("Do you want to delete this task?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    Step_Budget selected = TreeView_root.SelectedItem as Step_Budget;                    
                    Step_Budgets.Step_Budget_Selected = selected;
                    Step_Budgets.Remove_Step_Budget();
                    VMTabBudgetChart_Item_DrawChart();
                }
            }, (s, e) =>
            {
                e.OnCanExecute_SyncButtonVisibility(true, TreeView_root.SelectedItem != null);

            });
            Button_RefreshChart.Click += new RoutedEventHandler(Button_RefreshChart_Click);
            Button_ShowChartWindows.Click += new RoutedEventHandler(Button_ShowChartWindows_Click);
        }
        VMTabBudgetChart chartWindow = new VMTabBudgetChart();
        void Button_ShowChartWindows_Click(object sender, RoutedEventArgs e)
        {
            Action showNewWindow = () =>
            {
                Window w = new Window() { Title = "Bibget Chart", WindowState = WindowState.Maximized };
                w.Content = chartWindow;
                chartWindow.Tag = w;
                chartWindow.DrawChart(); ;
                w.Show();
            };
            if (chartWindow.Tag != null)
            {
                Window wOld = chartWindow.Tag as Window;
                if (wOld != null)
                {
                    if (!Application.Current.Windows.Cast<Window>().Contains(wOld))
                    {
                        wOld.Content = null;
                        showNewWindow();
                    }
                    else
                    {
                        wOld.Activate();
                    }
                }
            }
            else
            {
                showNewWindow();
            }
        }
        public void VMTabBudgetChart_Item_DrawChart()
        {
            VMTabBudgetChart_Item.DrawChart();
            if (chartWindow.Tag != null)
            {
                chartWindow.DrawChart();
            }
        }
        void Button_RefreshChart_Click(object sender, RoutedEventArgs e)
        {
            VMTabBudgetChart_Item_DrawChart();
        }
        VMTabBudget_Data Step_Budgets = null;//new VMTabBudget_Data();
    }
    public class VMTabBudget_Data : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private VMTabBudget_Data()
        { 
        }
        public VMTabBudget_Data(Step_Budget item)
        {
            RootInance = item;
        }
        #region _Step_Budgets
        Step_Budget RootInance= null;
        ObservableCollection<Step_Budget> _Step_Budgets = null;
        public ObservableCollection<Step_Budget> Step_Budgets
        {
            get
            {
                if (_Step_Budgets == null)
                {
                    _Step_Budgets = new ObservableCollection<Step_Budget>();                    
                }
                if (_Step_Budgets!= null && _Step_Budgets.Count == 0 && RootInance != null)
                    _Step_Budgets.Add(RootInance);
                return _Step_Budgets;

            }
            set
            {
                if (_Step_Budgets != value)
                {
                    _Step_Budgets = value;
                    SendPropertyChanged("Step_Budgets");
                }
            }
        }

        #endregion

        #region _Step_Budget_Selected
       Step_Budget _Step_Budget_Selected = null;
        public Step_Budget Step_Budget_Selected
        {
            get
            {

                return _Step_Budget_Selected;

            }
            set
            {
                if (_Step_Budget_Selected != value)
                {
                    _Step_Budget_Selected = value;
                    SendPropertyChanged("Step_Budget_Selected");
                }
            }
        }

        #endregion

        #region Action
        public void Add_Step_Budget(Step_Budget sb)
        {
            if (Step_Budget_Selected != null)
            {
                
                Step_Budget_Selected.Add_Budget(sb);
                
            }
        }
        public void Remove_Step_Budget()
        {
            var selected = Step_Budget_Selected;
            if (selected != null)
            {
                var parent = Step_Budget_Selected.Parent;
                if (parent == null)
                {
                    if (Step_Budgets.Contains(selected))
                        Step_Budgets.Remove(selected);
                }
                else
                {
                    parent.Remove_BudgetSub(selected);
                }
            }

        }
        #endregion

    }
}
